package com.todo.todolist.Controller.Main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.todo.todolist.Entity.ResultData;
import com.todo.todolist.Entity.TodoList;
import com.todo.todolist.Repository.TodoListRepository;
import com.todo.todolist.Service.TodoListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/")
public class MainController {
    @Resource
    private TodoListRepository todoListRepository;

    @Resource
    private TodoListService todoListService;

    Logger logger = LoggerFactory.getLogger(getClass());

    @GetMapping(path = "/index")
    public @ResponseBody
    Iterable<TodoList> getAllTodoList() {
        return todoListRepository.findAllOrderByCreate_time();
    }

    private String getJSession(HttpServletRequest request) {
        String jSession = "";
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().contains("JSESSION")) {
                    jSession = cookie.getValue();
                    logger.info(cookie.getName() + "=" + cookie.getValue());
                }
            }
        }
        return jSession;
    }

    @GetMapping("/")
    public String showIndex(ModelMap modelMap, HttpSession httpSession, HttpServletRequest request) {
        Object value = httpSession.getAttribute("guest");
        if (value == null) {
            logger.info("guest=null");
        } else {
            logger.info("guest=has");
        }

        String jSession = getJSession(request);


        Iterable<TodoList> todoLists = todoListRepository.findByJ_session(jSession);
        modelMap.addAttribute("todoLists", todoLists);
        return "index";
    }

    @PostMapping(value = "/add")
    public @ResponseBody
    ResultData addNewTodo(@RequestBody TodoList todoList) {
        TodoList saveRes = todoListService.add(todoList);
        return ResultData.success("添加成功",saveRes);
    }

    @DeleteMapping(value = "/delete/{id}")
    public @ResponseBody
    ResultData deleteTodoById(@PathVariable String id) {
        todoListService.delete(id);
        return ResultData.success();
    }
//    @PostMapping("/add")
//    public String addNewTodo(@ModelAttribute TodoList todoList, ModelMap modelMap, HttpServletRequest request, HttpSession httpSession) {
//        String jSession = getJSession(request);
//        todoList.setContent("");
//        todoList.setIs_done(0);
//        todoList.setJ_session(jSession);
//        todoListService.add(todoList);
//        Iterable<TodoList> todoLists = todoListRepository.findByJ_session(jSession);
//        modelMap.addAttribute("todoLists", todoLists);
//
//        Object value = httpSession.getAttribute("guest");
//        if(value == null){
//            logger.info("guest=null");
//        }else {
//            logger.info("guest=has");
//        }
//        httpSession.setAttribute("guest", todoLists);
//        return "index";
//    }
}
